﻿Imports Entities
Imports DataAccess

Public Class ItemBL
    Public Shared Function List() As List(Of ITEMES)
        Dim lista As New List(Of ITEMES)
        lista = ItemDAL.List
        Return lista
    End Function

    Public Shared Function ListByProveedorId(ByVal zId As Long) As List(Of ITEMES)
        Dim lista As New List(Of ITEMES)
        lista = ItemDAL.ListByProveedorId(zId)
        Return lista
    End Function
    Public Shared Function Save(ByVal zObject As ITEMES) As Boolean
        If ItemDAL.Exist(zObject.ITEM) Then
            ItemDAL.Update(zObject)
            Return True
        Else
            Return ItemDAL.Create(zObject)
        End If
    End Function

    Public Shared Function Delete(ByVal zId As Long) As Boolean
        Return False
    End Function

    Public Shared Function SearchById(ByVal zId As Long) As ITEMES
        Dim obj As New ITEMES
        obj = ItemDAL.SearchById(zId)
        If Not IsNothing(obj) Then
            Return obj
        Else
            Return Nothing
        End If
    End Function
    Public Shared Function UpdateStockVenta(ByVal zId As Long, ByVal zPrecio As Long) As Boolean
        Dim obj As New ITEMES
        obj = ItemDAL.SearchById(zId)
        If Not IsNothing(obj) Then
            obj.PRECIOVENTA = zPrecio
            obj.STOCK -= 1
            ItemDAL.Update(obj)
            Return True
        Else
            Return False
        End If
    End Function
    Public Shared Function UpdateStockMasivo(ByVal zId As Long, ByVal zIncrementoStock As Long) As Boolean
        Dim obj As New ITEMES
        obj = ItemDAL.SearchById(zId)
        If Not IsNothing(obj) Then
            obj.STOCK += zIncrementoStock
            ItemDAL.Update(obj)
            Return True
        Else
            Return False
        End If
    End Function
    Public Shared Function GetKilos(ByVal zId As Long) As Long
        Dim obj As New ITEMES
        obj = ItemDAL.SearchById(zId)
        If Not IsNothing(obj) Then
            Return obj.CANTIDADKILOS
        Else
            Return 0
        End If
    End Function
    Public Shared Function GetDescripcion(ByVal zId As Long) As String
        Dim obj As New ITEMES
        obj = ItemDAL.SearchById(zId)
        If Not IsNothing(obj) Then
            Return obj.DESCRIPCION
        Else
            Return 0
        End If
    End Function
    Public Shared Function GetGarantia(ByVal zId As Long) As Long
        Dim obj As New ITEMES
        obj = ItemDAL.SearchById(zId)
        If Not IsNothing(obj) Then
            Return obj.GARANTIA
        Else
            Return 0
        End If
    End Function
    Public Shared Function SetStock(ByVal zId As Long, ByVal zStock As Long) As ITEMES
        Dim obj As New ITEMES
        obj = ItemDAL.SearchById(zId)
        If Not IsNothing(obj) Then
            obj.STOCK = zStock
            Return ItemDAL.Update(obj)

        Else
            Return Nothing
        End If
    End Function
    Public Shared Function ListP() As List(Of ItemesP)
        Dim lista As New List(Of ITEMES)
        Dim listaF As New List(Of ItemesP)
        lista = ItemDAL.List

        If Not IsNothing(lista) Then
        
            For Each rec In lista
                Dim p As New ItemesP
                With p
                    .ItemId = rec.ITEM
                    .Garantia = GarantiaBL.GetDescripcion(rec.GARANTIA)
                    .Proveedor = ProveedorBL.GetNombre(rec.PROVEEDOR)
                    .Descripcion = rec.DESCRIPCION
                    .CantidadKilos = rec.CANTIDADKILOS
                    .Stock = rec.STOCK
                    .PrecioVenta = rec.PRECIOVENTA
                End With
                listaF.Add(p)
            Next
        End If
        Return listaF
    End Function

    Public Shared Function ListByGarantiaId(ByVal zId As Long) As List(Of ITEMES)
        Dim lista As New List(Of ITEMES)
        lista = ItemDAL.ListByGarantiaId(zId)
        Return lista
    End Function
End Class
